local AbsActExcel = require("excel.absAct")
local Lang = require("common.Lang")
local ObjHuman = require("core.ObjHuman")
local Msg = require("core.Msg")
local Broadcast = require("broadcast.Broadcast")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local BuyLogic = require("topup.BuyLogic")
local AbsActDefine = require("absAct.AbsActDefine")
local AbsActLogic = require("absAct.AbsActLogic")
local Log = require("common.Log")
local YunYingLogic = require("yunying.YunYingLogic")

--
ABS_PREMIUM_GIFT_LIST_ACTID = { }
ABS_PREMIUM_GIFT_LIST_BUYID = { }
function initAfterHot()
    ABS_PREMIUM_GIFT_LIST_ACTID = {}
    for k, v in pairs(AbsActExcel.premiumGift) do
        ABS_PREMIUM_GIFT_LIST_ACTID[v.actId] = ABS_PREMIUM_GIFT_LIST_ACTID[v.actId] or { }
        ABS_PREMIUM_GIFT_LIST_ACTID[v.actId][#ABS_PREMIUM_GIFT_LIST_ACTID[v.actId] + 1] = k
        if v.buyID ~= 0 then
            ABS_PREMIUM_GIFT_LIST_BUYID[v.buyID] = k
        end
    end
end

--
function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

--
function isOpen(human, YYInfo, funcConfig)
    return AbsActLogic.isStarted(human, funcConfig.funcID)
end

--
function isRed(human, YYInfo, funcConfig)
    local ret, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    if ret == true then
        local id = funcConfig.funcID
        local absActConfig = AbsActExcel.absActivity[id]
        if not absActConfig then return end
        AbsActLogic.checkAbsActClean(human, id)
        local absAct = human.db.absAct[id]
        for k, giftId in pairs(ABS_PREMIUM_GIFT_LIST_ACTID[absActConfig.actId]) do
            local v = AbsActExcel.premiumGift[giftId]
            if v then
                if not absAct.premiumCnt or not absAct.premiumCnt[giftId] then
                    return true
                end      
            end
        end     
        return false  
    end
    return false
end

--
function getLeftTime(human, YInfo, funcConfig)
    local ret, endTime, startTime = AbsActLogic.isStarted(human, funcConfig.funcID)
    if ret == true then
        return endTime - os.time()
    else
        return 0
    end
end


function premiumGift(human, id, buyConf, isFirst, cnt)
    local state = AbsActLogic.isStarted(human, id)
    local absConfig = AbsActExcel.absActivity[id]
    if absConfig == nil or not state then return end

    -- 存在多个特惠礼包 同时   开放
    local premiumID = ABS_PREMIUM_GIFT_LIST_BUYID[buyConf.id]
    if not premiumID then return end

    local config = AbsActExcel.premiumGift[premiumID]
    if not config then return end

    if config.actId ~= absConfig.actId then return end

    AbsActLogic.checkAbsActClean(human, id)

    human.db.absAct[id].premiumCnt = human.db.absAct[id].premiumCnt or { }
    local nowBuyCnt = human.db.absAct[id].premiumCnt[premiumID] or 0

    -- 判断是否达到购买上限
    if nowBuyCnt >= config.cnt then
        Broadcast.sendErr(human, Lang.HERO_BAG_BUY_CAP_NO_CNT)
        return
    end

    -- 当金币购买之后超过最大金币时，不允许购买
    for j = 1, #config.reward do
        local itemID = config.reward[j][1]
        local itemCnt = config.reward[j][2]
        if itemID == ItemDefine.ITEM_JINBI_ID then
            if not ObjHuman.canAddJinbi(human, itemCnt) then
                return
            end
        end
    end

    -- 增加已购买次数
    human.db.absAct[id].premiumCnt[premiumID] = nowBuyCnt + 1
    -- 发放物品
    -- local items = { }
    BagLogic.addItemList(human, AbsActExcel.premiumGift[premiumID].reward, "premium_reward")

    Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)
    AbsActLogic.actDetailQuery(human, id)
end

function getAndSendMsg(human, id, actId)
    local now = os.time()
    local startedFlag, endTime, startTime = AbsActLogic.isStarted(human, id)
    if not startedFlag then return end
    local absActConfig = AbsActExcel.absActivity[id]
    if not absActConfig then return end

    local absAct = human.db.absAct[id]
    if not absAct then return end

    local msgRet = Msg.gc.GC_ABS_ACT_PREMIUM_GIFT_QUERY
    msgRet.templateId = absActConfig.panelID
    msgRet.ad = absActConfig.adIcon
    msgRet.absActId = id
    msgRet.status = absAct.status or 0

    local index = 0
    for k, giftId in pairs(ABS_PREMIUM_GIFT_LIST_ACTID[absActConfig.actId]) do
        local v = AbsActExcel.premiumGift[giftId]
        if v then
            index = index + 1
            Log.write(Log.LOGID_TEST, "getAndSendMsg index="..index.." actId="..absActConfig.actId.." id="..id.." giftId="..giftId)
            for j = 1, #v.reward do
                Grid.makeItem(msgRet.premiumGiftMsg[index].item[j], v.reward[j][1], v.reward[j][2])
            end
            msgRet.premiumGiftMsg[index].item[0] = #v.reward
            msgRet.premiumGiftMsg[index].cnt = absAct.premiumCnt and absAct.premiumCnt[giftId] or 0
            msgRet.premiumGiftMsg[index].id = giftId
            msgRet.premiumGiftMsg[index].maxCnt = v.cnt
            msgRet.premiumGiftMsg[index].icon = v.icon or 0
            msgRet.premiumGiftMsg[index].name = v.name or ""

            local buyID = v.buyID
            msgRet.premiumGiftMsg[index].buyMsg[0] = 0
            msgRet.premiumGiftMsg[index].needItem[0] = 0
            if v.buyID == 0 then
                local len = 0
                for _, item in ipairs(v.needItem) do
                    len = len + 1
                    Grid.makeItem(msgRet.premiumGiftMsg[index].needItem[len], item[1], item[2])
                end
                msgRet.premiumGiftMsg[index].needItem[0] = len
            else
                BuyLogic.fontBuyItem(human, msgRet.premiumGiftMsg[index].buyMsg[1], buyID)
                msgRet.premiumGiftMsg[index].buyMsg[0] = 1
            end
        end
    end
    msgRet.premiumGiftMsg[0] = index
    msgRet.startTime = startTime
    msgRet.endTime = endTime
    Msg.send(msgRet, human.fd)
end


function giftBuy(human, id, premiumID)
    local state = AbsActLogic.isStarted(human, id)
    if state ~= true then return Broadcast.sendErr(human, Lang.ACT_WAS_OVER) end
    local absConfig = AbsActExcel.absActivity[id]
    if not absConfig then return end

    AbsActLogic.checkAbsActClean(human, id)

    -- 存在多个特惠礼包 同时   开放
    local config = AbsActExcel.premiumGift[premiumID]
    if config.buyID ~= 0 then return end

    -- 初始化已购买次数
    human.db.absAct[id].premiumCnt = human.db.absAct[id].premiumCnt or { }
    local nowBuyCnt = human.db.absAct[id].premiumCnt[premiumID] or 0
    -- 判断是否达到购买上限
    if nowBuyCnt >= config.cnt then
        Broadcast.sendErr(human, Lang.HERO_BAG_BUY_CAP_NO_CNT)
        return
    end

    for _, item in ipairs(config.needItem) do
        local itemID = item[1]
        local itemCnt = item[2]
        if not BagLogic.checkItemCnt(human, itemID, itemCnt) then
            Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
            return
        end
    end

    -- 当金币购买之后超过最大金币时，不允许购买
    for j = 1, #config.reward do
        local itemID = config.reward[j][1]
        local itemCnt = config.reward[j][2]
        if itemID == ItemDefine.ITEM_JINBI_ID then
            if not ObjHuman.canAddJinbi(human, itemCnt) then
                return
            end
        end
    end

    for _, item in ipairs(config.needItem) do
        local itemID = item[1]
        local itemCnt = item[2]
        BagLogic.delItem(human, itemID, itemCnt, "premium_reward")
    end

    -- 增加已购买次数
    human.db.absAct[id].premiumCnt[premiumID] = nowBuyCnt + 1
    -- 发放物品
    -- local items = { }
    BagLogic.addItemList(human, AbsActExcel.premiumGift[premiumID].reward, "premium_reward")

    Broadcast.sendErr(human, Lang.ITEM_BUY_SUCCESS)
    AbsActLogic.actDetailQuery(human, id)
end